/*
 * Copyright (c) 2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/sys/IApplication.h"

namespace alkes {

#if ALKES_API_WIN32
#define AL_DEVICE_TYPE_SUITABLE AL_DEVICE_TYPE_DIRECTX9
#else
#define AL_DEVICE_TYPE_SUITABLE AL_DEVICE_TYPE_OPENGL
#endif

IApplication::IApplication(const char* title, uint16_t width, uint16_t height)
: driver_(0)
, title_(title)
, width_(width)
, height_(height)
, device_type_(AL_DEVICE_TYPE_SUITABLE)
, factory_(0)
, debug_(0)
{
#if (ALKES_API_WIN32 && defined(ALKES_DEBUG))
    _CrtSetDbgFlag(
        _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)|
        _CRTDBG_ALLOC_MEM_DF|
        _CRTDBG_CHECK_ALWAYS_DF|
        /*_CRTDBG_CHECK_CRT_DF|*/
        /*_CRTDBG_DELAY_FREE_MEM_DF|*/
        _CRTDBG_LEAK_CHECK_DF);
#endif
}

IApplication::~IApplication()
{
}

IAppDriver* IApplication::addDriver(IAppDriver* driver)
{
    IAppDriver* old_driver = driver_;
    driver_ = driver;
    driver_->registerApplicaiton(this);
    return old_driver;
}

GraphicsDeviceFactory IApplication::setDeviceFactory(GraphicsDeviceFactory factory)
{
    GraphicsDeviceFactory old_factory = factory_;

    factory_ = factory;

    return old_factory;
}

GraphicsDeviceFactory IApplication::getDeviceFactory() const
{
    return factory_;
}

int IApplication::run(int argc, char** argv)
{
    int ret = 2;
    if (initialize(argc, argv))
    {
        onMainloop();

        finalize();
    }

    return ret;
}

bool IApplication::initialize(int argc, char* argv[])
{
    return driver_->invokePreInit(argc, argv);
}

void IApplication::finalize()
{
}

void IApplication::onMainloop()
{
}

void IApplication::setTitle(const char* title)
{
    title_ = title;
}

const char* IApplication::getTitle() const
{
    static const char* DEFAULT_TITLE = "alkes application";
    return title_ ? title_ : DEFAULT_TITLE;
}

void IApplication::setSize(uint16_t width, uint16_t height)
{
    width_ = width;
    height_ = height;
}

uint16_t IApplication::getWidth() const
{
    return width_;
}

uint16_t IApplication::getHeight() const
{
    return height_;
}

void IApplication::setDeviceType(DeviceType type)
{
    device_type_ = type;
}


DeviceType IApplication::getDeviceType() const
{
    return device_type_;
}

void IApplication::setDebugMode(bool debug_mode)
{
    debug_ = debug_mode;
}

bool IApplication::isDebugMode() const
{
    return debug_;
}

NativeWindow IApplication::getNativeWindow() const
{
    return 0;
}

bool IApplication::invokeInit(IGraphicsDevice* device)
{
    return driver_->invokeInit(device);
}

void IApplication::invokeFin()
{
    driver_->invokeFin();
}

void IApplication::invokeDraw()
{
    driver_->invokeDraw();
}

void IApplication::invokeResize(uint16_t new_width, uint16_t new_height)
{
    driver_->invokeResize(new_width, new_height);
}

void IApplication::invokeDirty(const Rect& rect)
{
    driver_->invokeDirty(rect);
}

void IApplication::invokePress(KeyEvent key)
{
    driver_->invokePress(key);
}

void IApplication::invokeRelease(KeyEvent key)
{
    driver_->invokeRelease(key);
}

void IApplication::invokeDeviceEvent(uint16_t device_no, uint64_t value)
{
    driver_->invokeDeviceEvent(device_no, value);
}

void IApplication::invokeMouseMove(uint16_t x, uint16_t y)
{
    driver_->invokeMouseMove(x, y);
}

}
